package SimpleMailer;

use strict;
use warnings;

use Carp;
use utf8;
use MIME::Base64;

use lib '../lib';
use GlobalConfig;

sub send_mail {
    my $to_mail = shift;
    my $subject = shift;
    my $body = shift;
    my $from_name = shift || GlobalConfig::ADMIN_NAME;
    my $from_mail = shift || GlobalConfig::FROM_MAIL;

    $subject = encode($subject);
    $from_name = encode($from_name);

    open MAIL, "| /usr/lib/sendmail -t -F'$from_name' -f'$from_mail' -ODeliveryMode=background " 
        or die "Could not open sendmail: $!";

    my $mail = 
qq{To: $to_mail
Subject: $subject
Reply-to: $to_mail
MIME-Version: 1.0
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: 7bit

$body
}; 

    print MAIL $mail;
    close MAIL or die "Error closing sendmail: $!";
}

sub encode {
    my $string = shift;
    my $base64 = encode_base64($string, '');
    return "=?UTF-8?B?$base64?=";
}

1;
